import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;


public class ThreadedServer {
	
	private final int PORT = 9997;
	
	private ServerSocket server;
	private Socket client;
	
	private int anzahl_client_threads = 0;
		
	public ThreadedServer() {
	}
	
	
	public void startServer(){
		System.out.print("Server starten ");
		try {
			
			server = new ServerSocket(PORT);
			for(int i=0,j=(new Random().nextInt(3)+2);i<=j;i++){ 
				System.out.print(". ");
				Thread.sleep(500);
			}
			System.out.println("OK");
			
			while(true){
				
				System.out.println("> Warten auf Clientanfrage...");
				client = server.accept();
				System.out.println("\t> Client: " + client + ".");
				
				new ClientThread(client,++anzahl_client_threads).start();
				
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
}
